import java.util.ArrayList;

public class TaskQueue {
	ArrayList<Task> tasks = new ArrayList<Task>();

	public synchronized void put(Task request) {
		tasks.add(request);
		notifyAll();
	}

	public synchronized Task take() {
		if (tasks.size() == 0) {
			try {
				wait();
			} catch (InterruptedException e) {
			}
		}
		notifyAll();
		return tasks.remove(0);
	}

	public synchronized void undo() {
		ArrayList<Task> tmps = new ArrayList<Task>(tasks);
		tasks.clear();
		for (Task request : tmps) {
			request.undo();
		}
	}
}
